<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>richtextspellchecker.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_richtextspellchecker.js.html">richtextspellchecker.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Rich text spell checker implementation.
<a name="line17"></a> *
<a name="line18"></a> * @author eae@google.com (Emil A Eklund)
<a name="line19"></a> * @author sergeys@google.com (Sergey Solyanik)
<a name="line20"></a> * @see ../demos/richtextspellchecker.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.RichTextSpellChecker&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line28"></a>goog.require(&#39;goog.events&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.string.StringBuffer&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.AbstractSpellChecker&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.AbstractSpellChecker.AsyncResult&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Rich text spell checker implementation.
<a name="line38"></a> *
<a name="line39"></a> * @param {goog.spell.SpellCheck} handler Instance of the SpellCheckHandler
<a name="line40"></a> *     support object to use. A single instance can be shared by multiple editor
<a name="line41"></a> *     components.
<a name="line42"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
<a name="line43"></a> * @constructor
<a name="line44"></a> * @extends {goog.ui.AbstractSpellChecker}
<a name="line45"></a> */
<a name="line46"></a>goog.ui.RichTextSpellChecker = function(handler, opt_domHelper) {
<a name="line47"></a>  goog.ui.AbstractSpellChecker.call(this, handler, opt_domHelper);
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * String buffer for use in reassembly of the original text.
<a name="line51"></a>   * @type {goog.string.StringBuffer}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.workBuffer_ = new goog.string.StringBuffer();
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * Bound async function (to avoid rebinding it on every call).
<a name="line58"></a>   * @type {Function}
<a name="line59"></a>   * @private
<a name="line60"></a>   */
<a name="line61"></a>  this.boundContinueAsyncFn_ = goog.bind(this.continueAsync_, this);
<a name="line62"></a>};
<a name="line63"></a>goog.inherits(goog.ui.RichTextSpellChecker, goog.ui.AbstractSpellChecker);
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * Root node for rich editor.
<a name="line68"></a> * @type {Node}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.ui.RichTextSpellChecker.prototype.rootNode_;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Current node where spell checker has interrupted to go to the next stack
<a name="line76"></a> * frame.
<a name="line77"></a> * @type {Node}
<a name="line78"></a> * @private
<a name="line79"></a> */
<a name="line80"></a>goog.ui.RichTextSpellChecker.prototype.currentNode_;
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Counter of inserted elements. Used in processing loop to attempt to preserve
<a name="line85"></a> * existing nodes if they contain no misspellings.
<a name="line86"></a> * @type {number}
<a name="line87"></a> * @private
<a name="line88"></a> */
<a name="line89"></a>goog.ui.RichTextSpellChecker.prototype.elementsInserted_ = 0;
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Number of words to scan to precharge the dictionary.
<a name="line94"></a> * @type {number}
<a name="line95"></a> * @private
<a name="line96"></a> */
<a name="line97"></a>goog.ui.RichTextSpellChecker.prototype.dictionaryPreScanSize_ = 1000;
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Class name for word spans.
<a name="line102"></a> * @type {string}
<a name="line103"></a> */
<a name="line104"></a>goog.ui.RichTextSpellChecker.prototype.wordClassName =
<a name="line105"></a>    goog.getCssName(&#39;goog-spellcheck-word&#39;);
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * DomHelper to be used for interacting with the editable document/element.
<a name="line110"></a> *
<a name="line111"></a> * @type {goog.dom.DomHelper|undefined}
<a name="line112"></a> * @private
<a name="line113"></a> */
<a name="line114"></a>goog.ui.RichTextSpellChecker.prototype.editorDom_;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Tag name porition of the marker for the text that does not need to be checked
<a name="line119"></a> * for spelling.
<a name="line120"></a> *
<a name="line121"></a> * @type {Array.&lt;string|undefined&gt;}
<a name="line122"></a> */
<a name="line123"></a>goog.ui.RichTextSpellChecker.prototype.excludeTags;
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * CSS Style text for invalid words. As it&#39;s set inside the rich edit iframe
<a name="line128"></a> * classes defined in the parent document are not availble, thus the style is
<a name="line129"></a> * set inline.
<a name="line130"></a> * @type {string}
<a name="line131"></a> */
<a name="line132"></a>goog.ui.RichTextSpellChecker.prototype.invalidWordCssText =
<a name="line133"></a>    &#39;background: yellow;&#39;;
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Creates the initial DOM representation for the component.
<a name="line138"></a> *
<a name="line139"></a> * @throws {Error} Not supported. Use decorate.
<a name="line140"></a> * @see #decorate
<a name="line141"></a> * @override
<a name="line142"></a> */
<a name="line143"></a>goog.ui.RichTextSpellChecker.prototype.createDom = function() {
<a name="line144"></a>  throw Error(&#39;Render not supported for goog.ui.RichTextSpellChecker.&#39;);
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Decorates the element for the UI component.
<a name="line150"></a> *
<a name="line151"></a> * @param {Element} element Element to decorate.
<a name="line152"></a> * @override
<a name="line153"></a> */
<a name="line154"></a>goog.ui.RichTextSpellChecker.prototype.decorateInternal = function(element) {
<a name="line155"></a>  this.setElementInternal(element);
<a name="line156"></a>  if (element.contentDocument || element.contentWindow) {
<a name="line157"></a>    var doc = element.contentDocument || element.contentWindow.document;
<a name="line158"></a>    this.rootNode_ = doc.body;
<a name="line159"></a>    this.editorDom_ = goog.dom.getDomHelper(doc);
<a name="line160"></a>  } else {
<a name="line161"></a>    this.rootNode_ = element;
<a name="line162"></a>    this.editorDom_ = goog.dom.getDomHelper(element);
<a name="line163"></a>  }
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/** @override */
<a name="line168"></a>goog.ui.RichTextSpellChecker.prototype.enterDocument = function() {
<a name="line169"></a>  goog.ui.RichTextSpellChecker.superClass_.enterDocument.call(this);
<a name="line170"></a>  this.initSuggestionsMenu();
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Checks spelling for all text and displays correction UI.
<a name="line176"></a> * @override
<a name="line177"></a> */
<a name="line178"></a>goog.ui.RichTextSpellChecker.prototype.check = function() {
<a name="line179"></a>  this.blockReadyEvents();
<a name="line180"></a>  this.preChargeDictionary_(this.rootNode_, this.dictionaryPreScanSize_);
<a name="line181"></a>  this.unblockReadyEvents();
<a name="line182"></a>
<a name="line183"></a>  goog.events.listen(this.handler_, goog.spell.SpellCheck.EventType.READY,
<a name="line184"></a>                     this.onDictionaryCharged_, true, this);
<a name="line185"></a>  this.handler_.processPending();
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Processes nodes recursively.
<a name="line191"></a> *
<a name="line192"></a> * @param {Node} node Node to start with.
<a name="line193"></a> * @param {number} words Max number of words to process.
<a name="line194"></a> * @private
<a name="line195"></a> */
<a name="line196"></a>goog.ui.RichTextSpellChecker.prototype.preChargeDictionary_ = function(node,
<a name="line197"></a>                                                                       words) {
<a name="line198"></a>  while (node) {
<a name="line199"></a>    var next = this.nextNode_(node);
<a name="line200"></a>    if (this.isExcluded_(node)) {
<a name="line201"></a>      node = next;
<a name="line202"></a>      continue;
<a name="line203"></a>    }
<a name="line204"></a>    if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line205"></a>      if (node.nodeValue) {
<a name="line206"></a>        words -= this.populateDictionary(node.nodeValue, words);
<a name="line207"></a>        if (words &lt;= 0) {
<a name="line208"></a>          return;
<a name="line209"></a>        }
<a name="line210"></a>      }
<a name="line211"></a>    } else if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line212"></a>      if (node.firstChild) {
<a name="line213"></a>        next = node.firstChild;
<a name="line214"></a>      }
<a name="line215"></a>    }
<a name="line216"></a>    node = next;
<a name="line217"></a>  }
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Starts actual processing after the dictionary is charged.
<a name="line223"></a> * @param {goog.events.Event} e goog.spell.SpellCheck.EventType.READY event.
<a name="line224"></a> * @private
<a name="line225"></a> */
<a name="line226"></a>goog.ui.RichTextSpellChecker.prototype.onDictionaryCharged_ = function(e) {
<a name="line227"></a>  e.stopPropagation();
<a name="line228"></a>  goog.events.unlisten(this.handler_, goog.spell.SpellCheck.EventType.READY,
<a name="line229"></a>                       this.onDictionaryCharged_, true, this);
<a name="line230"></a>
<a name="line231"></a>  // Now actually do the spell checking.
<a name="line232"></a>  this.clearWordElements();
<a name="line233"></a>  this.initializeAsyncMode();
<a name="line234"></a>  this.elementsInserted_ = 0;
<a name="line235"></a>  var result = this.processNode_(this.rootNode_);
<a name="line236"></a>  if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line237"></a>    goog.Timer.callOnce(this.boundContinueAsyncFn_);
<a name="line238"></a>    return;
<a name="line239"></a>  }
<a name="line240"></a>  this.finishAsyncProcessing();
<a name="line241"></a>  this.finishCheck_();
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * Continues asynchrnonous spell checking.
<a name="line247"></a> * @private
<a name="line248"></a> */
<a name="line249"></a>goog.ui.RichTextSpellChecker.prototype.continueAsync_ = function() {
<a name="line250"></a>  var result = this.continueAsyncProcessing();
<a name="line251"></a>  if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line252"></a>    goog.Timer.callOnce(this.boundContinueAsyncFn_);
<a name="line253"></a>    return;
<a name="line254"></a>  }
<a name="line255"></a>  result = this.processNode_(this.currentNode_);
<a name="line256"></a>  if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line257"></a>    goog.Timer.callOnce(this.boundContinueAsyncFn_);
<a name="line258"></a>    return;
<a name="line259"></a>  }
<a name="line260"></a>  this.finishAsyncProcessing();
<a name="line261"></a>  this.finishCheck_();
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Finalizes spelling check.
<a name="line267"></a> * @private
<a name="line268"></a> */
<a name="line269"></a>goog.ui.RichTextSpellChecker.prototype.finishCheck_ = function() {
<a name="line270"></a>  delete this.currentNode_;
<a name="line271"></a>  this.handler_.processPending();
<a name="line272"></a>
<a name="line273"></a>  if (!this.isVisible()) {
<a name="line274"></a>    goog.events.listen(this.rootNode_, goog.events.EventType.CLICK,
<a name="line275"></a>                       this.onWordClick_, false, this);
<a name="line276"></a>  }
<a name="line277"></a>  goog.ui.RichTextSpellChecker.superClass_.check.call(this);
<a name="line278"></a>};
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>/**
<a name="line282"></a> * Finds next node in our enumeration of the tree.
<a name="line283"></a> *
<a name="line284"></a> * @param {Node} node The node to which we&#39;re computing the next node for.
<a name="line285"></a> * @return {Node} The next node or null if none was found.
<a name="line286"></a> * @private
<a name="line287"></a> */
<a name="line288"></a>goog.ui.RichTextSpellChecker.prototype.nextNode_ = function(node) {
<a name="line289"></a>  while (node != this.rootNode_) {
<a name="line290"></a>    if (node.nextSibling) {
<a name="line291"></a>      return node.nextSibling;
<a name="line292"></a>    }
<a name="line293"></a>    node = node.parentNode;
<a name="line294"></a>  }
<a name="line295"></a>  return null;
<a name="line296"></a>};
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/**
<a name="line300"></a> * Determines if the node is text node without any children.
<a name="line301"></a> *
<a name="line302"></a> * @param {Node} node The node to check.
<a name="line303"></a> * @return {boolean} Whether the node is a text leaf node.
<a name="line304"></a> * @private
<a name="line305"></a> */
<a name="line306"></a>goog.ui.RichTextSpellChecker.prototype.isTextLeaf_ = function(node) {
<a name="line307"></a>  return node != null &amp;&amp;
<a name="line308"></a>         node.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line309"></a>         !node.firstChild;
<a name="line310"></a>};
<a name="line311"></a>
<a name="line312"></a>
<a name="line313"></a>/** @override */
<a name="line314"></a>goog.ui.RichTextSpellChecker.prototype.setExcludeMarker = function(marker) {
<a name="line315"></a>  if (marker) {
<a name="line316"></a>    if (typeof marker == &#39;string&#39;) {
<a name="line317"></a>      marker = [marker];
<a name="line318"></a>    }
<a name="line319"></a>
<a name="line320"></a>    this.excludeTags = [];
<a name="line321"></a>    this.excludeMarker = [];
<a name="line322"></a>    for (var i = 0; i &lt; marker.length; i++) {
<a name="line323"></a>      var parts = marker[i].split(&#39;.&#39;);
<a name="line324"></a>      if (parts.length == 2) {
<a name="line325"></a>        this.excludeTags.push(parts[0]);
<a name="line326"></a>        this.excludeMarker.push(parts[1]);
<a name="line327"></a>      } else {
<a name="line328"></a>        this.excludeMarker.push(parts[0]);
<a name="line329"></a>        this.excludeTags.push(undefined);
<a name="line330"></a>      }
<a name="line331"></a>    }
<a name="line332"></a>  }
<a name="line333"></a>};
<a name="line334"></a>
<a name="line335"></a>
<a name="line336"></a>/**
<a name="line337"></a> * Determines if the node is excluded from checking.
<a name="line338"></a> *
<a name="line339"></a> * @param {Node} node The node to check.
<a name="line340"></a> * @return {boolean} Whether the node is excluded.
<a name="line341"></a> * @private
<a name="line342"></a> */
<a name="line343"></a>goog.ui.RichTextSpellChecker.prototype.isExcluded_ = function(node) {
<a name="line344"></a>  if (this.excludeMarker &amp;&amp; node.className) {
<a name="line345"></a>    for (var i = 0; i &lt; this.excludeMarker.length; i++) {
<a name="line346"></a>      var excludeTag = this.excludeTags[i];
<a name="line347"></a>      var excludeClass = this.excludeMarker[i];
<a name="line348"></a>      var isExcluded = !!(excludeClass &amp;&amp;
<a name="line349"></a>          node.className.indexOf(excludeClass) != -1 &amp;&amp;
<a name="line350"></a>          (!excludeTag || node.tagName == excludeTag));
<a name="line351"></a>      if (isExcluded) {
<a name="line352"></a>        return true;
<a name="line353"></a>      }
<a name="line354"></a>    }
<a name="line355"></a>  }
<a name="line356"></a>  return false;
<a name="line357"></a>};
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Processes nodes recursively.
<a name="line362"></a> *
<a name="line363"></a> * @param {Node} node Node where to start.
<a name="line364"></a> * @return {goog.ui.AbstractSpellChecker.AsyncResult|undefined} Result code.
<a name="line365"></a> * @private
<a name="line366"></a> */
<a name="line367"></a>goog.ui.RichTextSpellChecker.prototype.processNode_ = function(node) {
<a name="line368"></a>  delete this.currentNode_;
<a name="line369"></a>  while (node) {
<a name="line370"></a>    var next = this.nextNode_(node);
<a name="line371"></a>    if (this.isExcluded_(node)) {
<a name="line372"></a>      node = next;
<a name="line373"></a>      continue;
<a name="line374"></a>    }
<a name="line375"></a>    if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line376"></a>      var deleteNode = true;
<a name="line377"></a>      if (node.nodeValue) {
<a name="line378"></a>        var currentElements = this.elementsInserted_;
<a name="line379"></a>        var result = this.processTextAsync(node, node.nodeValue);
<a name="line380"></a>        if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) {
<a name="line381"></a>          // This markes node for deletion (empty nodes get deleted couple
<a name="line382"></a>          // of lines down this function). This is so our algorithm terminates.
<a name="line383"></a>          // In this case the node may be needlessly recreated, but it
<a name="line384"></a>          // happens rather infrequently and saves a lot of code.
<a name="line385"></a>          node.nodeValue = &#39;&#39;;
<a name="line386"></a>          this.currentNode_ = node;
<a name="line387"></a>          return result;
<a name="line388"></a>        }
<a name="line389"></a>        // If we did not add nodes in processing, the current element is still
<a name="line390"></a>        // valid. Let&#39;s preserve it!
<a name="line391"></a>        if (currentElements == this.elementsInserted_) {
<a name="line392"></a>          deleteNode = false;
<a name="line393"></a>        }
<a name="line394"></a>      }
<a name="line395"></a>      if (deleteNode) {
<a name="line396"></a>        goog.dom.removeNode(node);
<a name="line397"></a>      }
<a name="line398"></a>    } else if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line399"></a>      // If this is a spell checker element...
<a name="line400"></a>      if (node.className == this.wordClassName) {
<a name="line401"></a>        // First, reconsolidate the text nodes inside the element - editing
<a name="line402"></a>        // in IE splits them up.
<a name="line403"></a>        var runner = node.firstChild;
<a name="line404"></a>        while (runner) {
<a name="line405"></a>          if (this.isTextLeaf_(runner)) {
<a name="line406"></a>            while (this.isTextLeaf_(runner.nextSibling)) {
<a name="line407"></a>              // Yes, this is not super efficient in IE, but it will almost
<a name="line408"></a>              // never happen.
<a name="line409"></a>              runner.nodeValue += runner.nextSibling.nodeValue;
<a name="line410"></a>              goog.dom.removeNode(runner.nextSibling);
<a name="line411"></a>            }
<a name="line412"></a>          }
<a name="line413"></a>          runner = runner.nextSibling;
<a name="line414"></a>        }
<a name="line415"></a>        // Move its contents out and reprocess it on the next iteration.
<a name="line416"></a>        if (node.firstChild) {
<a name="line417"></a>          next = node.firstChild;
<a name="line418"></a>          while (node.firstChild) {
<a name="line419"></a>            node.parentNode.insertBefore(node.firstChild, node);
<a name="line420"></a>          }
<a name="line421"></a>        }
<a name="line422"></a>        // get rid of the empty shell.
<a name="line423"></a>        goog.dom.removeNode(node);
<a name="line424"></a>      } else {
<a name="line425"></a>        if (node.firstChild) {
<a name="line426"></a>          next = node.firstChild;
<a name="line427"></a>        }
<a name="line428"></a>      }
<a name="line429"></a>    }
<a name="line430"></a>    node = next;
<a name="line431"></a>  }
<a name="line432"></a>};
<a name="line433"></a>
<a name="line434"></a>
<a name="line435"></a>/**
<a name="line436"></a> * Processes word.
<a name="line437"></a> *
<a name="line438"></a> * @param {Node} node Node containing word.
<a name="line439"></a> * @param {string} word Word to process.
<a name="line440"></a> * @param {goog.spell.SpellCheck.WordStatus} status Status of the word.
<a name="line441"></a> * @protected
<a name="line442"></a> * @override
<a name="line443"></a> */
<a name="line444"></a>goog.ui.RichTextSpellChecker.prototype.processWord = function(node, word,
<a name="line445"></a>                                                              status) {
<a name="line446"></a>  node.parentNode.insertBefore(this.createWordElement_(word, status), node);
<a name="line447"></a>  this.elementsInserted_++;
<a name="line448"></a>};
<a name="line449"></a>
<a name="line450"></a>
<a name="line451"></a>/**
<a name="line452"></a> * Processes recognized text and separators.
<a name="line453"></a> *
<a name="line454"></a> * @param {Node} node Node containing separator.
<a name="line455"></a> * @param {string} text Text to process.
<a name="line456"></a> * @protected
<a name="line457"></a> * @override
<a name="line458"></a> */
<a name="line459"></a>goog.ui.RichTextSpellChecker.prototype.processRange = function(node, text) {
<a name="line460"></a>  // The text does not change, it only gets split, so if the lengths are the
<a name="line461"></a>  // same, the text is the same, so keep the existing node.
<a name="line462"></a>  if (node.nodeType == goog.dom.NodeType.TEXT &amp;&amp; node.nodeValue.length ==
<a name="line463"></a>      text.length) {
<a name="line464"></a>    return;
<a name="line465"></a>  }
<a name="line466"></a>
<a name="line467"></a>  node.parentNode.insertBefore(this.editorDom_.createTextNode(text), node);
<a name="line468"></a>  this.elementsInserted_++;
<a name="line469"></a>};
<a name="line470"></a>
<a name="line471"></a>
<a name="line472"></a>/**
<a name="line473"></a> * @override
<a name="line474"></a> * @suppress {accessControls}
<a name="line475"></a> */
<a name="line476"></a>goog.ui.RichTextSpellChecker.prototype.createWordElement_ = function(word,
<a name="line477"></a>                                                                     status) {
<a name="line478"></a>  var parameters = this.getElementProperties(status);
<a name="line479"></a>  var el = /** @type {HTMLSpanElement} */ (this.editorDom_.createDom(&#39;span&#39;,
<a name="line480"></a>      parameters, word));
<a name="line481"></a>  this.registerWordElement_(word, el);
<a name="line482"></a>  return el;
<a name="line483"></a>};
<a name="line484"></a>
<a name="line485"></a>
<a name="line486"></a>/**
<a name="line487"></a> * Updates or replaces element based on word status.
<a name="line488"></a> * @see goog.ui.AbstractSpellChecker.prototype.updateElement_
<a name="line489"></a> *
<a name="line490"></a> * Overridden from AbstractSpellChecker because we need to be mindful of
<a name="line491"></a> * deleting the currentNode_ - this can break our pending processing.
<a name="line492"></a> *
<a name="line493"></a> * @param {Element} el Word element.
<a name="line494"></a> * @param {string} word Word to update status for.
<a name="line495"></a> * @param {goog.spell.SpellCheck.WordStatus} status Status of word.
<a name="line496"></a> * @protected
<a name="line497"></a> * @override
<a name="line498"></a> */
<a name="line499"></a>goog.ui.RichTextSpellChecker.prototype.updateElement = function(el, word,
<a name="line500"></a>    status) {
<a name="line501"></a>  if (status == goog.spell.SpellCheck.WordStatus.VALID &amp;&amp; el !=
<a name="line502"></a>      this.currentNode_ &amp;&amp; el.nextSibling != this.currentNode_) {
<a name="line503"></a>    this.removeMarkup(el);
<a name="line504"></a>  } else {
<a name="line505"></a>    goog.dom.setProperties(el, this.getElementProperties(status));
<a name="line506"></a>  }
<a name="line507"></a>};
<a name="line508"></a>
<a name="line509"></a>
<a name="line510"></a>/**
<a name="line511"></a> * Hides correction UI.
<a name="line512"></a> * @override
<a name="line513"></a> */
<a name="line514"></a>goog.ui.RichTextSpellChecker.prototype.resume = function() {
<a name="line515"></a>  goog.ui.RichTextSpellChecker.superClass_.resume.call(this);
<a name="line516"></a>
<a name="line517"></a>  this.restoreNode_(this.rootNode_);
<a name="line518"></a>
<a name="line519"></a>  goog.events.unlisten(this.rootNode_, goog.events.EventType.CLICK,
<a name="line520"></a>                       this.onWordClick_, false, this);
<a name="line521"></a>};
<a name="line522"></a>
<a name="line523"></a>
<a name="line524"></a>/**
<a name="line525"></a> * Processes nodes recursively, removes all spell checker markup, and
<a name="line526"></a> * consolidates text nodes.
<a name="line527"></a> *
<a name="line528"></a> * @param {Node} node node on which to recurse.
<a name="line529"></a> * @private
<a name="line530"></a> */
<a name="line531"></a>goog.ui.RichTextSpellChecker.prototype.restoreNode_ = function(node) {
<a name="line532"></a>  while (node) {
<a name="line533"></a>    if (this.isExcluded_(node)) {
<a name="line534"></a>      node = node.nextSibling;
<a name="line535"></a>      continue;
<a name="line536"></a>    }
<a name="line537"></a>    // Contents of the child of the element is usually 1 text element, but the
<a name="line538"></a>    // user can actually add multiple nodes in it during editing. So we move
<a name="line539"></a>    // all the children out, prepend, and reprocess (pointer is set back to
<a name="line540"></a>    // the first node that&#39;s been moved out, and the loop repeats).
<a name="line541"></a>    if (node.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp; node.className ==
<a name="line542"></a>        this.wordClassName) {
<a name="line543"></a>      var firstElement = node.firstChild;
<a name="line544"></a>      var next;
<a name="line545"></a>      for (var child = firstElement; child; child = next) {
<a name="line546"></a>        next = child.nextSibling;
<a name="line547"></a>        node.parentNode.insertBefore(child, node);
<a name="line548"></a>      }
<a name="line549"></a>      next = firstElement || node.nextSibling;
<a name="line550"></a>      goog.dom.removeNode(node);
<a name="line551"></a>      node = next;
<a name="line552"></a>      continue;
<a name="line553"></a>    }
<a name="line554"></a>    // If this is a chain of text elements, we&#39;re trying to consolidate it.
<a name="line555"></a>    var textLeaf = this.isTextLeaf_(node);
<a name="line556"></a>    if (textLeaf) {
<a name="line557"></a>      var textNodes = 1;
<a name="line558"></a>      var next = node.nextSibling;
<a name="line559"></a>      while (this.isTextLeaf_(node.previousSibling)) {
<a name="line560"></a>        node = node.previousSibling;
<a name="line561"></a>        ++textNodes;
<a name="line562"></a>      }
<a name="line563"></a>      while (this.isTextLeaf_(next)) {
<a name="line564"></a>        next = next.nextSibling;
<a name="line565"></a>        ++textNodes;
<a name="line566"></a>      }
<a name="line567"></a>      if (textNodes &gt; 1) {
<a name="line568"></a>        this.workBuffer_.append(node.nodeValue);
<a name="line569"></a>        while (this.isTextLeaf_(node.nextSibling)) {
<a name="line570"></a>          this.workBuffer_.append(node.nextSibling.nodeValue);
<a name="line571"></a>          goog.dom.removeNode(node.nextSibling);
<a name="line572"></a>        }
<a name="line573"></a>        node.nodeValue = this.workBuffer_.toString();
<a name="line574"></a>        this.workBuffer_.clear();
<a name="line575"></a>      }
<a name="line576"></a>    }
<a name="line577"></a>    // Process child nodes, if any.
<a name="line578"></a>    if (node.firstChild) {
<a name="line579"></a>      this.restoreNode_(node.firstChild);
<a name="line580"></a>    }
<a name="line581"></a>    node = node.nextSibling;
<a name="line582"></a>  }
<a name="line583"></a>};
<a name="line584"></a>
<a name="line585"></a>
<a name="line586"></a>/**
<a name="line587"></a> * Returns desired element properties for the specified status.
<a name="line588"></a> *
<a name="line589"></a> * @param {goog.spell.SpellCheck.WordStatus} status Status of the word.
<a name="line590"></a> * @return {Object} Properties to apply to word element.
<a name="line591"></a> * @protected
<a name="line592"></a> * @override
<a name="line593"></a> */
<a name="line594"></a>goog.ui.RichTextSpellChecker.prototype.getElementProperties =
<a name="line595"></a>    function(status) {
<a name="line596"></a>  return {
<a name="line597"></a>    &#39;class&#39;: this.wordClassName,
<a name="line598"></a>    &#39;style&#39;: (status == goog.spell.SpellCheck.WordStatus.INVALID) ?
<a name="line599"></a>        this.invalidWordCssText : &#39;&#39;
<a name="line600"></a>  };
<a name="line601"></a>};
<a name="line602"></a>
<a name="line603"></a>
<a name="line604"></a>/**
<a name="line605"></a> * Handler for click events.
<a name="line606"></a> *
<a name="line607"></a> * @param {goog.events.BrowserEvent} event Event object.
<a name="line608"></a> * @private
<a name="line609"></a> */
<a name="line610"></a>goog.ui.RichTextSpellChecker.prototype.onWordClick_ = function(event) {
<a name="line611"></a>  var target = /** @type {Element} */ (event.target);
<a name="line612"></a>  if (event.target.className == this.wordClassName &amp;&amp;
<a name="line613"></a>      this.handler_.checkWord(goog.dom.getTextContent(target)) ==
<a name="line614"></a>      goog.spell.SpellCheck.WordStatus.INVALID) {
<a name="line615"></a>
<a name="line616"></a>    this.showSuggestionsMenu(target, event);
<a name="line617"></a>
<a name="line618"></a>    // Prevent document click handler from closing the menu.
<a name="line619"></a>    event.stopPropagation();
<a name="line620"></a>  }
<a name="line621"></a>};
<a name="line622"></a>
<a name="line623"></a>
<a name="line624"></a>/** @override */
<a name="line625"></a>goog.ui.RichTextSpellChecker.prototype.disposeInternal = function() {
<a name="line626"></a>  goog.ui.RichTextSpellChecker.superClass_.disposeInternal.call(this);
<a name="line627"></a>  this.rootNode_ = null;
<a name="line628"></a>  this.editorDom_ = null;
<a name="line629"></a>};
</pre>


</body>
</html>
